package com.example.sprintboot.controller;

import com.example.sprintboot.security.UserDetailsImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/debug")
public class DebugController {

    @GetMapping("/auth")
    public Map<String, Object> getAuthInfo() {
        Map<String, Object> result = new HashMap<>();
        
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof UserDetailsImpl) {
            UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
            result.put("authenticated", true);
            result.put("username", userDetails.getUsername());
            result.put("userId", userDetails.getUserId());
            result.put("authorities", userDetails.getAuthorities());
        } else {
            result.put("authenticated", false);
            result.put("principal", authentication != null ? authentication.getPrincipal() : null);
        }
        
        return result;
    }

    @GetMapping("/test")
    public String testEndpoint() {
        return "测试端点正常工作";
    }
} 